/*
 * Copyright 2010    Sylvere RICHARD
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.infotrafficlux.client.place;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.infotrafficlux.client.place.PlacePresenter.IPlaceView;
import com.google.infotrafficlux.client.service.PlaceServiceAsync;
import com.google.infotrafficlux.shared.GwtPlace;
import com.mvp4g.client.annotation.InjectService;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.PresenterInterface;

@Presenter(view = PlaceView.class)
public class PlacePresenter implements
		PresenterInterface<IPlaceView, PlaceEventBus> {

	public interface IPlaceView {

		public void clearTable();

		void setData(List<GwtPlace> result);

		HasClickHandlers getPrevious();

		HasClickHandlers getNext();

		Command getSortCommand();

		void setSortCommand(Command sortCommand);

	}

	private PlaceServiceAsync service;
	private PlaceEventBus eventBus;
	private IPlaceView view;

	private int startIndex = 0;

	public PlacePresenter() {

	}

	@Override
	public void bind() {
		update(startIndex);

		view.getPrevious().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if (startIndex >= 10) {
					startIndex -= 10;
					update(startIndex);
				}

			}
		});

		view.getNext().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				startIndex += 10;
				update(startIndex);

			}
		});

	}

	private void update(int startIndex) {
		AsyncCallback<List<GwtPlace>> callback = new AsyncCallback<List<GwtPlace>>() {
			public void onSuccess(List<GwtPlace> result) {

				view.clearTable();
				view.setData(result);
			}

			public void onFailure(Throwable caught) {
				Window.alert("Error when invoking the pageable data service	: "
						+ caught.getMessage());
			}
		};
		service.getPlaces(startIndex, 10, callback);
	}

	@InjectService
	public void setService(PlaceServiceAsync service) {
		this.service = service;
	}

	PlaceServiceAsync getService() {
		return this.service;
	}

	@Override
	public void setEventBus(PlaceEventBus eventBus) {
		this.eventBus = eventBus;
	}

	@Override
	public PlaceEventBus getEventBus() {
		return eventBus;
	}

	@Override
	public void setView(IPlaceView view) {
		this.view = view;
	}

	@Override
	public IPlaceView getView() {
		return view;
	}

	@Override
	public void bindIfNeeded() {
		bind();

	}

	public void onPlaceCreated(GwtPlace p) {

	}

	public void onPlaceDeleted(GwtPlace p) {

	}
}
